package com.lj.mcore.service;

import com.lj.mcore.dao.base.BaseAppDAO;
import com.lj.mcore.entity.User;
import com.lj.mcore.utils.CommUtils;
import com.lj.mcore.utils.PageBean;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional
@Repository
public class UserServiceImpl implements IUserService{

    @Resource(name="userDao")
    private BaseAppDAO<User> userDao;


    @Override
    public boolean save(User paramUser) {
        userDao.save(paramUser);
        return false;
    }

    @Override
    public boolean delete(Long paramLong) {
        return false;
    }

    @Override
    public boolean update(User paramUser) {
        return false;
    }

    @Override
    public User getObjById(Long paramLong) {
        return userDao.findById(paramLong);
    }

    @Override
    public User getSingleResult(Map<String, Object> map) {
        return userDao.getSingleResult(map);
    }

    @Override
    public List<User> query(String paramString, Map paramMap, int paramInt1, int paramInt2) {
        return userDao.query(paramString, paramMap, paramInt1, paramInt2);
    }

    @Override
    public PageBean getUserList(int pageSize, int page, Map<String, String> query) {
        StringBuffer sb = new StringBuffer("from User obj where obj.deleteStatus = false ");
        if(query != null && query.size() >0 ){

            if(!CommUtils.null2String(query.get("userName")).equals("")){
                sb.append(" and obj.userName = '"+query.get("userName")+"' ");
            }

            if(!CommUtils.null2String(query.get("trueName")).equals("")){
                sb.append(" and obj.trueName = '"+query.get("trueName")+"' ");
            }

            if(!CommUtils.null2String(query.get("email")).equals("")){
                sb.append(" and obj.email = '"+query.get("email")+"' ");
            }
        }
        sb.append(" and obj.parent.id is null order by obj.addTime desc");

        String hql = sb.toString();

        List listNum = this.query("select count(obj.id) "+hql,null,0,1);
        int allRow=0;
        if (listNum!=null){
            allRow = CommUtils.null2Int(listNum.get(0));
        }
        int totalPage = PageBean.countTotalPage(pageSize, allRow);
        final int offset = PageBean.countOffset(pageSize, page);
        final int length = pageSize;
        final int currentPage = PageBean.countCurrentPage(page);
        List list = this.query(hql,null, offset, length);
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(currentPage);
        pageBean.setAllRow(allRow);
        pageBean.setTotalPage(totalPage);
        pageBean.setList(list);
        pageBean.init();
        return pageBean;
    }
}
